package org.uoss.webapp.framework.struts2.action;

import org.uoss.applications.framework.dao.model.QueryModel;
import org.uoss.applications.framework.dao.model.ResultSetModel;
import org.uoss.core.util.ValidationUtil;
import org.uoss.webapp.framework.http.HttpUtil;
import org.uoss.webapp.framework.struts2.object.JSONResultModel;

public abstract class PaginationAction<T> extends GenericAction<T>{

	private static final long serialVersionUID = -2145698832300214954L;
	private JSONResultModel<T> result;
	private String status;
	private Integer pageIndex = 1;
	private Integer pageSize = 30;
	
	protected void toJSONObject(ResultSetModel<T> results){
		result = new JSONResultModel<T>();
		if(results!=null){
			result.setTotal(results.getTotalCount());
			result.setRows(results.getItems());
		}
	}

	protected QueryModel getQueryModel(){
		String jspPageIndex = HttpUtil.getHttpServletRequest().getParameter("pageIndex");
		String jspPageSize = HttpUtil.getHttpServletRequest().getParameter("pageSize");
		this.setPageSize(ValidationUtil.isEmpty(jspPageSize) ? this.getPageSize() : Integer.parseInt(jspPageSize));
		this.setPageIndex(ValidationUtil.isEmpty(jspPageIndex) ? this.getPageIndex() : Integer.parseInt(jspPageIndex));
		QueryModel queryModel = new QueryModel();
		queryModel.setPageIndex(this.getPageIndex());
		queryModel.setPageSize(this.getPageSize());
		return queryModel;
	}
	
	public JSONResultModel<T> getResult()
	{
		return result;
	}
	public void setResult(JSONResultModel<T> results)
	{
		this.result = results;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Integer getPageIndex()
	{
		return pageIndex;
	}

	public void setPageIndex(Integer pageIndex)
	{
		this.pageIndex = pageIndex;
	}
	public Integer getPageSize()
	{
		return pageSize;
	}
	public void setPageSize(Integer pageSize)
	{
		this.pageSize = pageSize;
	}
}
